package cn.openea.perpartner.meily.web.service.importandexport;

import cn.openea.perpartner.common.core.exception.ServiceException;
import cn.openea.perpartner.common.core.utils.poi.ExcelUtil;
import cn.openea.perpartner.meily.constant.CommonConstants;
import cn.openea.perpartner.meily.constant.enums.ExpressCompany;
import cn.openea.perpartner.meily.constant.enums.LogisticsModule;
import cn.openea.perpartner.meily.web.entity.dto.ImportErrMsgDTO;
import cn.openea.perpartner.meily.web.entity.dto.TrialEvaluationSubShipmentDTO;
import cn.openea.perpartner.meily.web.entity.vo.ExportDataVo;
import cn.openea.perpartner.meily.web.service.ITrialEvaluationService;
import cn.openea.perpartner.meily.web.service.logistics.businessModule.LogisticsModlueHandler;
import cn.openea.perpartner.meily.web.service.logistics.businessModule.LogisticsModuleService;
import com.alibaba.fastjson.JSONObject;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ysk
 * @version 1.0.1
 * @Description 测评批量发货
 * @createTime 2023年05月16日 17:18:00
 */
@Slf4j
@Service(value = CommonConstants.IMPORTANDEXPORT.EVALUATION_BATCH_DELIVERY)
public class EvaluationBatchDeliveryService extends ImportAndExportAbstractService {

    @Resource
    private ITrialEvaluationService trialEvaluationService;
    @Resource
    private LogisticsModlueHandler logisticsModlueHandler;

    @Override
    public void exportExcel(ExportDataVo exportDataVo, HttpServletResponse response) {
        JSONObject queryVO = exportDataVo.getQueryVO();
        String id = queryVO.getString("id");
        if (StringUtils.isBlank(id)) {
            throw new ServiceException("工单号不能为空");
        }
        // 默认查询待发货数据
        queryVO.put("orderStatus", "待发货");
        queryVO.put("pageSize", "1000");
        JSONObject resObj = trialEvaluationService.getJoinList(queryVO);
        if (200 != resObj.getInteger("code")) {
            throw new ServiceException("数据异常");
        }
        List<TrialEvaluationSubShipmentDTO> shipmentDTOS = new ArrayList<>();
        String rows = JSONObject.toJSONString(resObj.get("rows"));
        List<JSONObject> jsonObjectList = JSONObject.parseArray(rows, JSONObject.class);
        for (JSONObject object : jsonObjectList) {
            TrialEvaluationSubShipmentDTO subShipmentDTO = JSONObject.parseObject(JSONObject.toJSONString(object), TrialEvaluationSubShipmentDTO.class);
            subShipmentDTO.setTaskNumber(object.getString("mainTaskNumber"));
            shipmentDTOS.add(subShipmentDTO);
        }
        ExcelUtil<TrialEvaluationSubShipmentDTO> util = new ExcelUtil<>(TrialEvaluationSubShipmentDTO.class);
        util.exportExcel(response, shipmentDTOS, "测评批量发货模板批量导入模板.xlsx");
    }

    @Override
    public ImportErrMsgDTO handleSingleExcel(@NonNull Object obj) {
        TrialEvaluationSubShipmentDTO subShipmentDTO = (TrialEvaluationSubShipmentDTO) obj;
        try {
            if (StringUtils.isBlank(subShipmentDTO.getConsigneeName()) || StringUtils.isBlank(subShipmentDTO.getCourierNumber())) {
                throw new ServiceException("快递公司或快递单号为空");
            }
            ExpressCompany expressCompany = ExpressCompany.findMostSimilarElement(subShipmentDTO.getCourierName());
            LogisticsModuleService service = logisticsModlueHandler.getService(LogisticsModule.EVALUATION);
            service.logisticsChange(expressCompany, subShipmentDTO.getDocUnid(), subShipmentDTO.getCourierNumber());
        } catch (Exception e) {
            return ImportErrMsgDTO.builder()
                    .id(subShipmentDTO.getDocUnid())
                    .errMsg(e.getMessage())
                    .build();
        }
        return null;
    }
}
